package com.easy.dn.ui.main.adapter;

import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.easy.common.commonutils.ImageLoaderUtils;
import com.easy.common.commonutils.TimeUtil;
import com.easy.dn.api.ApiConstants;
import com.easy.dn.bean.SearchResumeBean;
import com.easy.dn.utils.DnUtils;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;

import dn.easy.com.easyto.R;


/**
 * Created by 丁宁
 * on 2017/6/26.
 */

public class SearchResumeAdapter extends BaseQuickAdapter<SearchResumeBean.RowsBean, BaseViewHolder> {

    public SearchResumeAdapter(@LayoutRes int layoutResId, @Nullable List<SearchResumeBean.RowsBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, SearchResumeBean.RowsBean bean) {
        helper.setText(R.id.tv_resume_name, bean.getJobName());

        //月薪
        if (0 == bean.getSalaryBegin() && 0 == bean.getSalaryEnd()) {
            helper.setText(R.id.tv_salary, mContext.getResources().getString(R.string.chat_face));
        } else {
            helper.setText(R.id.tv_salary, "￥"
                    + bean.getSalaryBegin() + "-"
                    + bean.getSalaryEnd() + "/" + mContext.getResources().getString(R.string.month));
        }
        //dateFormatYMDHMS 刷新时间
        String refreshTime = TimeUtil.formatData(TimeUtil.dateFormatM_D, bean.getRefreshTime());
        //头像
        ImageLoaderUtils.display(mContext, (ImageView) helper.getView(R.id.iv_search_resume_headImg), ApiConstants.URL + bean.getImg(), R.drawable.default_head);
        //判断时间是否为今天 今天显示为今天 其余 MM-dd
        if (TimeUtil.isToday(TimeUtil.formatData(TimeUtil.dateFormatYMDHMS, bean.getRefreshTime()))) {
            helper.setText(R.id.tv_refresh_time, mContext.getResources().getString(R.string.today));
        } else {
            helper.setText(R.id.tv_refresh_time, refreshTime);
        }
        //浏览量
        helper.setText(R.id.tv_see_count, bean.getMatchingCount() + "");
        //地点
        helper.setText(R.id.tv_address, bean.getCityName());
        // 显示语言
        String professionalLanguage = bean.getLanguagePro();
        String goodLanguage = bean.getLanguageGood();

        String[] professionalLanguageStr = {};
        String[] goodLanguageStr = {};
        List<String> lanList = new ArrayList<>();

        if (bean.getExpTime() > 1) {
            lanList.add(bean.getExpTime()+ mContext.getResources().getString(R.string.year_number));
        } else if (bean.getExpTime() == 1) {
            lanList.add(bean.getExpTime()+ mContext.getResources().getString(R.string.year_odd_number));
        }

        if (null != professionalLanguage && !"".equals(professionalLanguage)) {
            professionalLanguageStr = professionalLanguage.split(",");
        }

        if (null != goodLanguage && !"".equals(goodLanguage)) {
            goodLanguageStr = goodLanguage.split(",");
        }

        if (professionalLanguageStr != null && professionalLanguageStr.length > 0) {
            for (String lan : professionalLanguageStr) {
                lanList.add(DnUtils.getLanguage(lan));
            }
        }

        if (goodLanguageStr != null && goodLanguageStr.length > 0) {
            for (String lan : goodLanguageStr) {
                lanList.add(DnUtils.getLanguage(lan));
            }
        }
        List<String> newList = new ArrayList(new LinkedHashSet(lanList));
        LinearLayout lanLinear = helper.getView(R.id.lanLinear);
        lanLinear.removeAllViews();
        // 空间有限 只显示前四个
        for (int i = 0; i < newList.size(); i++) {
            TextView labelTextView = (TextView) LayoutInflater.from(mContext).inflate(R.layout.resume_list_label, null);
            labelTextView.setText(newList.get(i));
            if (i <= 4) {
                if (0 == i) {
                    lanLinear.addView(labelTextView);
                } else {
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT);
                    lp.setMargins(15, 0, 0, 0);
                    labelTextView.setLayoutParams(lp);
                    lanLinear.addView(labelTextView);
                }
            }
        }
    }
}
